package org.dromara.trace.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.trace.domain.vo.TraceCodeVo;
import org.dromara.trace.domain.bo.TraceCodeBo;
import org.dromara.trace.service.ITraceCodeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 溯源码
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trace/code")
public class TraceCodeController extends BaseController {

    private final ITraceCodeService traceCodeService;

    /**
     * 查询溯源码列表
     */
    @SaCheckPermission("trace:code:list")
    @GetMapping("/list")
    public TableDataInfo<TraceCodeVo> list(TraceCodeBo bo, PageQuery pageQuery) {
        return traceCodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出溯源码列表
     */
    @SaCheckPermission("trace:code:export")
    @Log(title = "溯源码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TraceCodeBo bo, HttpServletResponse response) {
        List<TraceCodeVo> list = traceCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "溯源码", TraceCodeVo.class, response);
    }

    /**
     * 获取溯源码详细信息
     *
     * @param codeId 主键
     */
    @SaCheckPermission("trace:code:query")
    @GetMapping("/{codeId}")
    public R<TraceCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long codeId) {
        return R.ok(traceCodeService.queryById(codeId));
    }

    /**
     * 新增溯源码
     */
    @SaCheckPermission("trace:code:add")
    @Log(title = "溯源码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TraceCodeBo bo) {
        return toAjax(traceCodeService.insertByBo(bo));
    }

    /**
     * 修改溯源码
     */
    @SaCheckPermission("trace:code:edit")
    @Log(title = "溯源码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TraceCodeBo bo) {
        return toAjax(traceCodeService.updateByBo(bo));
    }

    /**
     * 删除溯源码
     *
     * @param codeIds 主键串
     */
    @SaCheckPermission("trace:code:remove")
    @Log(title = "溯源码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] codeIds) {
        return toAjax(traceCodeService.deleteWithValidByIds(List.of(codeIds), true));
    }
}
